const { Controller } = require('egg');
const { ctxBodyFormatter } = require('../util/formatter');

class StudentController extends Controller {
  async index() {
    const { ctx, service } = this;
    const res = ctxBodyFormatter(await service.student.getAllStudent());
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async show() {
    const { ctx, service } = this;
    const student = { sId: ctx.params?.id };
    const res = ctxBodyFormatter(await service.student.getStudent(student));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async create() {
    const { ctx, service } = this;
    const student = ctx.request.body;
    const res = ctxBodyFormatter(await service.student.appendStudent(student));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async update() {
    const { ctx, service } = this;
    const student = ctx.request.body;
    const res = ctxBodyFormatter(await service.student.updateStudent(student));
    ctx.status = res.status;
    ctx.body = res.body;
  }

  async destroy() {
    const { ctx, service } = this;
    const student = { sId: ctx.params?.id };
    const res = ctxBodyFormatter(await service.student.destroyStudent(student));
    ctx.status = res.status;
    ctx.body = res.body;
  }
}

module.exports = StudentController;
